package deepbitview



/**
 * this is a service to access and trade with the mtgox online trading system.
 */
class MtgoxService {

    static transactional = true

    /**
     * returns the current account balnce
     * @return
     */
    def getCurrentBalance() {

        if (MtGoxAccessHandler.isConfigured()) {
            def values = executeQuery([name: MtGoxAccessHandler.getUserName(), pass: MtGoxAccessHandler.getPassword()], "/code/getFunds.php")

            def result = [usd: values.usds, coins: values.btcs]

            log.info("fetching current balance: ${values}")

            if (result.usd == null | result.coins == false) {
                return false
            }
            return result
        }
        else {
            return false
        }
    }

    /**
     *
     * places as buy order with the system
     * @param amount
     * @param amount
     * @return
     */
    def placeBuyOrder(double amountOfCoins, double priceToBuyAt) {

        if (MtGoxAccessHandler.isConfigured()) {
            def values = executeQuery([name: MtGoxAccessHandler.getUserName(), pass: MtGoxAccessHandler.getPassword(), amount: amountOfCoins, price: priceToBuyAt], "/code/buyBTC.php")

            log.info("place buy order: ${values}")

            return values.oid
        }
        else {
            return false
        }
    }

    /**
     * places a sell order
     * @param amountOfCoins
     * @param priceToSellAt
     * @return
     */
    def placeSellOrder(double amountOfCoins, double priceToSellAt) {
        if (MtGoxAccessHandler.isConfigured()) {
            def values = executeQuery([name: MtGoxAccessHandler.getUserName(), pass: MtGoxAccessHandler.getPassword(), amount: amountOfCoins, price: priceToSellAt], "/code/sellBTC.php")

            log.info("place sell order: ${values}")

            return values.oid
        }
        else {
            return false
        }
    }

    /**
     * cancles an order
     * @return
     */
    def cancelBuyOrder(String oid) {
        if (MtGoxAccessHandler.isConfigured()) {
            def values = executeQuery([name: MtGoxAccessHandler.getUserName(), pass: MtGoxAccessHandler.getPassword(), oid: oid, type: 2], "/code/cancelOrder.php")

            log.info("trying to cancel buyorder: ${values}")

            return values
        }
        else {
            return false
        }
    }

    /**
     * cancels an order
     * @return
     */
    def cancelSellOrder() {
        if (MtGoxAccessHandler.isConfigured()) {
            def values = executeQuery([name: MtGoxAccessHandler.getUserName(), pass: MtGoxAccessHandler.getPassword(), oid: oid, type: 2], "/code/cancelOrder.php")

            log.info("trying to cancel sell order: ${values}")

            if (values.error) {
                return false
            }

            return values
        }
        else {
            return false
        }
    }

    /**
     * lists all the open orders
     * @return
     */
    def listOpenOrders() {

        if (MtGoxAccessHandler.isConfigured()) {
            def values = executeQuery([name: MtGoxAccessHandler.getUserName(), pass: MtGoxAccessHandler.getPassword()], "/code/getOrders.php")

            log.info("received orders: ${values}")
            if (values.error) {
                return false
            }

            return values.orders
        }
        else {
            return false
        }
    }

    /**
     * executes a query
     * @param parameter
     * @param url
     * @return
     */
    private executeQuery(Map parameter, String path) {
        return JSONQueryUtil.executeQuery("https://mtgox.com/",parameter,path)
    }

}
